// JavaScript Document
((function(a){
	//插件本体
	var uploaderOne = function(opts){
		this.elebox = a.createElement('div');
		this.card = null;
		this.format = {},
		this.file = new $Q.file();//文件列表所有的信息
		this.msg = function(msg){
			this.format.msgbox.text(msg);
		}
		this.onSuccess = null;//上传成功
		this.result = null;//响应的文件信息
		this.onError = null;//上传失败
		
		//创建一个视频类型的UI
		this.createLocalVideoUI = function(){
			var ths = this;
			var file = this.file;
			file.ID = 'ui_' + Math.ceil( Math.random() * Math.pow(10,5));
			file.UI = function(){
				ths.format.box = a('<div style="display: inline-block;position: relative;height: 80px;margin: 5px;background-color: #ddd;width: 360px;"></div>');
				ths.format.process = a('<div style="position: absolute;top: 0px;left: 0px;right: 0px;bottom :0;background-color: #eaeaea;"></div>');
				ths.format.processbar = a('<div style="width:0%;height: 100%;background-color: #FF9800;"></div>');
				ths.format.msgbox = a('<div style="position: relative;margin: 0.25em;font-size: 12px;"></div>');
				var info = a('<div style="position:relative;margin: 0.25em;"><div style="word-break: break-all;text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 1;overflow: hidden;font-size: 12px;">文件名：'+this.file.name+'</div><div style="word-break: break-all;text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 1;overflow: hidden;font-size: 12px;">大小：'+this.file.size+'</div><div style="word-break: break-all;text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 1;overflow: hidden;font-size: 12px;">类型：'+this.file.type+'</div></div>');
				a(ths.format.box).append(ths.format.process);
				a(ths.format.box).append(info);
				a(ths.format.box).append(ths.format.msgbox);
				a(ths.format.process).html(ths.format.processbar);
				return ths.format.box;
			}
			
			file.onCompressStart = function(){
				ths.msg('视频压缩开始');
			}
			file.onCompressReady = function(){
				ths.msg('视频已获取内容，开始压缩');
			}
			file.onCompressProgress = function(index){
				ths.msg('视频压缩第'+index+'片段');	
			}
			file.onCompressEnd = function(){
				ths.msg('视频压缩完成');	
			}
			file.onUploadStart = function(){
				ths.msg('视频上传中');
				ths.format.processbar.css('width','0%');
			}
			file.onUploadEnd = function(){
				ths.format.processbar.css('width','100%');
				//上传成功
				//判断是否有下一个文件
				ths.msg('视频上传成功');
				ths.result = file.result;
				ths.onSuccess&&ths.onSuccess(ths.result);
				ths.hide();
			}
			file.onUploadErr = function(){
				ths.msg('视频上传失败');	
				ths.onError&&ths.onError();
			}
			file.onUploadSecStart = function(sec){
				
			}
			file.onUploadSecEnd = function(sec){
				var max = file.getSecnum();
				var num = file.getSecUploadednum();
				var w = num / max * 100;
				ths.format.processbar.css('width',w+'%');
			}
			file.onUploadSecErr = function(sec){
				var max = file.getSecnum();
				var num = file.getSecUploadednum();
				var w = num / max * 100;
				ths.format.processbarbar.css('width',w+'%');
			}
			return file.UI();
		}
		
		//创建一个其他类型的UI
		this.createLocalOtherUI = function(){
			$Q.alert('isOther');	
			return ;
		}
		
		
		//创建一个视频的本地UI
		this.createLocalUI = function(){
			if(this.file.isVideo()){
				//创建视频类型的文件
				return this.createLocalVideoUI();
			}else{
				//创建其他类型的文件
				return this.createLocalOtherUI();	
			}
		}
		
		
		//创建UI界面信息
		this.createUI = function(){
			var dom = this.createLocalUI();
            this.card = new a.card({
                format:['body'],
				content:dom,
                width:'370px',
                height:'92px',
                position:'br',
                zIndex:101,
            })
		}
		//卡片隐藏
		this.hide = function(){
			this.card.card.remove();
		}
		//执行上传文件的操作
		this.uploadFile = function(){
			var ths = this;
			this.file.createBynone(function(){
                ths.createUI();
				this.compress(function(){
					this.upload();	
				});
			});
		}
		
		//创建程序
		this.create = function(){


		}
		//检测后端服务是否正常
		//使用post的方式进行
		this.checkWebserver = function(){
			var config = a.config('UPLOAD');
			a.ajax({
				url:config.ACTION,
				data:config.PARAMS,
				type:'post',
				dataType:'json',
				success:function(rs){
					if(rs.status==1){
						
					}else{
						$Q.alert(rs.message);	
					}
				},
				error:function(){
					$Q.alert('上传控件初始化失败，请检查服务器端是否能正常返回');	
				},
			});
		}
		
		//检测后端是否可用
		this.checkWebserver();
		this.create();//创建代码
		
	};
	a.plugIn.register('videoUploaderOne',uploaderOne);
})(this.kuoqiJs))